package com.situ.mall.service.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.entity.Result;
import com.situ.mall.entity.User;
import com.situ.mall.mapper.UserMapper;
import com.situ.mall.service.IUserService;

@Service
public class UserServiceImpl implements IUserService{
	@Autowired
	private UserMapper userMapper;

	@Override
	public User login(String username, String password) {
		return userMapper.login(username, password);
	}

	@Override
	public void exportExcel(HttpServletResponse response) {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("用户列表");
		// 设置默认的列宽
		sheet.setDefaultColumnWidth(25);
		// 创建合并单元格对象
		CellRangeAddress cellRangeAddress = new CellRangeAddress(2, 4, 1, 4);
		sheet.addMergedRegion(cellRangeAddress);
		// 创建行：Row
		XSSFRow row = sheet.createRow(2);
		// 创建单元格：Cell (这个cell就是合并单元格的cell)
		XSSFCell cell = row.createCell(1);
		// 设置单元格的样式
		XSSFCellStyle style = workbook.createCellStyle();
		style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中
		style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中
		cell.setCellStyle(style);
		cell.setCellValue("用户列表");
		// 创建要输出的列
		String[] array = {"编号", "用户名", "手机号", "邮编"};
		XSSFRow rowHead = sheet.createRow(5);
		for (int i = 0; i < array.length; i++) {
			XSSFCell cellHead = rowHead.createCell(i + 1);
			cellHead.setCellValue(array[i]);
		}
		
		List<User> list = userMapper.selectAll();
		// 这个i是变量控制行
		for (int i = 0; i < list.size(); i++) {
			User user = list.get(i);
			XSSFRow userRow = sheet.createRow(6 + i);
			
			XSSFCell cellId = userRow.createCell(1);
			cellId.setCellValue(user.getId());
			
			XSSFCell cellName = userRow.createCell(2);
			cellName.setCellValue(user.getUsername());
			
			XSSFCell cellPhone = userRow.createCell(3);
			cellPhone.setCellValue(user.getPhone());
			
			XSSFCell cellEmail = userRow.createCell(4);
			cellEmail.setCellValue(user.getEmail());
		}
		
		// 以附件的形式返回给浏览器，要借助response
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		ServletOutputStream outputStream = null;
		try {
			response.setHeader("Content-Disposition",
					"attachment;filename=" + new String("用户列表.xlsx".getBytes(), "ISO-8859-1"));
			outputStream = response.getOutputStream();
			workbook.write(outputStream);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (workbook != null) {
				try {
					workbook.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (outputStream != null) {
					try {
						outputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	@Override
	public Result selectByPage(Integer page, Integer limit) {
		int count=userMapper.getTotalCount();
		int offset=(page-1)*limit;
		List<User>list=userMapper.selectByPage(offset,limit);
		return Result.success("", list ,count);
	}
	@Override
	public boolean deleteAll(Integer[] ids) {
		int count=0;
		for (Integer id : ids) {
			int c=userMapper.deleteByPrimaryKey(id);
			count+=c;
		}
		return count==ids.length?true:false;
	}
	@Override
	public boolean deleteById(Integer id) {
		int count=userMapper.deleteByPrimaryKey(id);
		return count == 1 ? true : false;
	}
	@Override
	public boolean insert(User user) {
		System.out.println(user);
		int count=userMapper.insert(user);
		return count == 1? true : false;
	}

}